package com.hongye.util;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.hongye.controller.KdniaoController;

/**
 * 快递鸟相关接口
 * @author liushaomin
 */
public class KDNiaoUtils {

	private static Logger log = Logger.getLogger(KdniaoController.class);
	private static final String API_ID = "1267423";
	private static final String API_KEY = "f45963c1-7ba3-42c0-ac81-7aae23b47b93";
	//即时查询请求URL
	private static final String RE_URL = "http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";
	//订阅推送请求URL--测试
//	private static final String REQ_URL = "http://testapi.kdniao.cc:8081/api/dist";
	//订阅推送请求URL--正式
	private static final String REQ_URL = "http://api.kdniao.cc/api/dist";
	
	
	/**
	 * 即时查询物流信息（<3000条/天）
	 * @author lifuqiang
	 * 2016-10-21
	 * @param expCode:快递公司编码
	 * @param expNo：快递单号
	 * @return {"EBusinessID": "1267346", "ShipperCode": "YD", "Success": true,"LogisticCode": "3932492051540","State": "3",
	 * 			"Traces": [{"AcceptTime": "2016-10-09 20:54:29", "AcceptStation": "到达：江苏海门市公司 已收件" } ]}
	 */
	public static String getOrderJson(String expCode, String expNo)  {
		String requestData= "{'OrderCode':'','ShipperCode':'" + expCode + "','LogisticCode':'" + expNo + "'}";
		Map<String, String> params = new HashMap<String, String>();
		try {
			params.put("RequestData", Utils.urlEncoder(requestData, "UTF-8"));
			params.put("EBusinessID", API_ID);
			params.put("RequestType", "1002");
			String dataSign=Utils.encrypt(requestData, API_KEY, "UTF-8");
			params.put("DataSign", Utils.urlEncoder(dataSign, "UTF-8"));
			params.put("DataType", "2");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		String result=HttpUtils.sendPost(RE_URL, params);
		if(log.isInfoEnabled()){log.info("result result="+result);}
		return result;
	}
	
	/**
	 * 单号识别（不返回物流轨迹）（<3000条/天）
	 * @author lifuqiang
	 * 2016-11-03
	 * @param expCode:快递公司编码
	 * @param expNo：快递单号
	 * @return {"EBusinessID": "1267346", "ShipperCode": "YD", "Success": true,"LogisticCode": "3932492051540","State": "3",
	 * 			"Traces": [{"AcceptTime": "2016-10-09 20:54:29", "AcceptStation": "到达：江苏海门市公司 已收件" } ]}
	 */
	public static String numberRecognition(String expNo){
		String requestData= "{'LogisticCode':'" + expNo + "'}";
		Map<String, String> params = new HashMap<String, String>();
		try {
			params.put("RequestData", Utils.urlEncoder(requestData, "UTF-8"));
			params.put("EBusinessID", API_ID);
			params.put("RequestType", "2002");
			String dataSign=Utils.encrypt(requestData, API_KEY, "UTF-8");
			params.put("DataSign", Utils.urlEncoder(dataSign, "UTF-8"));
			params.put("DataType", "2");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		String result=HttpUtils.sendPost(RE_URL, params);
		if(log.isInfoEnabled()){log.info("result result="+result);}
		return result;
	}
	
	/**
	 * 快递鸟订阅 接口
	 * @author lifuqiang
	 * 2016-10-21
	 * @param ShipperCode:快递公司编码
	 * @param LogisticCode：快递单号
	 * @return
	 */
	public static String orderTracesSubByJson(String ShipperCode, String LogisticCode) {
		String requestData="{'ShipperCode':'"+ShipperCode+"','LogisticCode':'"+LogisticCode+"'}";
		Map<String, String> params = new HashMap<String, String>();
		try {
			params.put("RequestData", Utils.urlEncoder(requestData, "UTF-8"));
			params.put("EBusinessID", API_ID);
			params.put("RequestType", "1008");
			String dataSign=Utils.encrypt(requestData, API_KEY, "UTF-8");
			params.put("DataSign", Utils.urlEncoder(dataSign, "UTF-8"));
			params.put("DataType", "2");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
//		String result=HttpUtils.post(REQ_URL, params);
		String result= HttpUtils.sendPost(REQ_URL, params);
		Map<String, Object> map = Utils.toMap(result);
		if(log.isInfoEnabled()){log.info("map map="+map);}
		return map.toString();
	}
}
